package com.example.diskfilesystem.directory;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

//目录项类
public class DirectoryEntry {
    public static final byte ATTR_swr = 0x02;//系统可读写文件
    public static final byte ATTR_sr = 0x03;//系统只读文件
    public static final byte ATTR_wr = 0x04;//普通可读写文件
    public static final byte ATTR_r = 0x05;//普通只读文件
    public static final byte ATTR_dir = 0x08;//目录


    private String fileName;//文件名
    private String fileType;//文件类型
    private byte attribute;//文件属性
    private byte startBlock;//文件起始盘块号
    private byte length;//文件长度（单位：盘块数）

    public DirectoryEntry(String fileName, String fileType, byte attribute, byte startBlock, byte length) {
        this.fileName = fileName;
        this.fileType = fileType;
        this.attribute = attribute;
        this.startBlock = startBlock;
        this.length = length;
    }

    //将字节数组解析为 DirectoryEntry对象
    public static DirectoryEntry fromBytes(byte[] bytes) {
        if (bytes.length != 8) throw new IllegalArgumentException("一个目录项应为8字节");
        //如果第一个字节是'$'，表示空目录项
        if (bytes[0] == (byte) '$') {
            return emptyEntry();
        }
        String name = new String(Arrays.copyOfRange(bytes, 0, 3), StandardCharsets.US_ASCII).trim();
        String type = new String(Arrays.copyOfRange(bytes, 3, 5), StandardCharsets.US_ASCII).trim();
        return new DirectoryEntry(name, type, bytes[5], bytes[6], bytes[7]);
    }

    //将DirectoryEntry对象解析为字节数组
    public byte[] toBytes() {
        byte[] out = new byte[8];
        Arrays.fill(out, (byte) ' ');
        byte[] nameBytes = fileName.getBytes(StandardCharsets.US_ASCII);
        System.arraycopy(nameBytes, 0, out, 0, Math.min(nameBytes.length, 3));
        byte[] typeBytes = fileType.getBytes(StandardCharsets.US_ASCII);
        System.arraycopy(typeBytes, 0, out, 3, Math.min(typeBytes.length, 2));

        out[5] = attribute;
        out[6] = startBlock;
        out[7] = length;
        return out;
    }

    //创建并返回空目录项
    public static DirectoryEntry emptyEntry() {
        return new DirectoryEntry("$", "", (byte) 0, (byte) 0, (byte) 0);
    }

    //判断是否为空目录项
    public boolean isEmpty() {
        return "$".equals(fileName);
    }

    //判断目录项属性（false表示该目录项记录的是文件）
    public boolean isDirectory() {
        return (attribute & ATTR_dir) == ATTR_dir;
    }

    public String getFileName() { return fileName; }
    public String getFileType() { return fileType; }
    public byte getAttribute() { return attribute; }
    public byte getStartBlock() { return startBlock; }
    public byte getLength() { return length; }

    public void setLength(byte length) { this.length = length; }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    public void setFileType(String fileType) {
        this.fileType = fileType;
    }
    public void setAttribute(byte attribute) {
        this.attribute = attribute;
    }
    public void setStartBlock(byte startBlock) {
        this.startBlock = startBlock;
    }
}
